//
//  SSRegisterNextController.m
//  textDemo
//
//  Created by Esther on 2017/2/18.
//  Copyright © 2017年 Esther. All rights reserved.
//  注册页面(手机号验证码)

#import "SSRegisterNextController.h"
#import "SSRegisterNextView.h"
#import "SSNetworkTools.h"
#import "UIView+Toast.h"

@interface SSRegisterNextController()
@property(nonatomic, strong) SSRegisterNextView *registerView;


@end

@implementation SSRegisterNextController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    [self requestCoderNumber];
    
}


#pragma mark -
#pragma mark - 搭建界面
- (void)setupUI{
    self.title = @"验证手机号";
    self.view.backgroundColor = [UIColor ss_colorWithRed:242 green:242 blue:242];
    [self.view addSubview:self.registerView];
    
    #pragma mark - 约束
    __weak typeof (self) weakSelf = self;
    [_registerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(200);
    }];
}

#pragma mark -
#pragma mark - 网络请求数据
// 请求验证码
- (void)requestCoderNumber{
    NSDictionary *parameters = @{@"MemberId" : _userInfo[@"userName"]};
    [[SSNetworkTools shared]ss_postLoadData:@"appMember/createCode.do" parameters:parameters completeBlock:^(id responseObject, NSError *error) {
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            //数据请求成功开始倒计时
            [self.registerView GCDTime];
            
        }else if ([responseObject[@"result"] isEqualToString:@"TelephoneExistError"]){
            [self.view makeToast:@"手机号已经被注册" duration:1.5 position:@"center"];
        }else{
            [self.view makeToast:@"验证码请求失败" duration:1.5 position:@"center"];
        }
    }];    
}
// 注册
- (void)registerMethod:(NSString *)code{
    NSDictionary *parameters = @{@"LoginName" : _userInfo[@"userName"],
                                 @"Lpassword" : _userInfo[@"password"],
                                 @"Code" : code,
                                 @"Telephone" : _userInfo[@"userName"]};
    
    [[SSNetworkTools shared] ss_getLoadData:@"appMember/appReistration.do"
                                 parameters:parameters
                              completeBlock:^(id responseObject, NSError *error) {

                                  if ([responseObject[@"result"] isEqualToString:@"success"]) {
                                     [self.view makeToast:@"注册成功" duration:1.5 position:@"center"];
                                      // 自动登录
                                      [self autoLogin];
                                      
                                  }else if ([responseObject[@"result"] isEqualToString:@"TelephoneExistError"]){
                                      [self.view makeToast:@"手机号已经被注册" duration:1.5 position:@"center"];
                                  }else if ([responseObject[@"result"] isEqualToString:@"codeError"]){
                                      [self.view makeToast:@"验证码错误" duration:1.5 position:@"center"];
                                  }else{
                                      [self.view makeToast:@"验证码请求失败" duration:1.5 position:@"center"];
                                  }
                                  if(error != nil){
                                      [self.view makeToast:@"请求失败,请检查网络" duration:1.5 position:@"center"];
                                  }
    }];
}

- (void)autoLogin{
    
    //    self.loginBlock(@{@"LoginName" : _userNameTextField.text,
    //                      @"Lpassword" : _passwordTextField.text
    NSDictionary *parameters = @{@"LoginName" : _userInfo[@"userName"],
                                 @"Lpassword" : _userInfo[@"password"]};
    
    [[SSNetworkTools shared] ss_getLoadData:@"appMember/appLogin.do" parameters:parameters completeBlock:^(id responseObject, NSError *error) {
        if (error != nil) {
            return;
        }
        
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            // 偏好设置
            [[NSUserDefaults standardUserDefaults] setObject:responseObject forKey:@"ISLOGIN"];
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
    
}
#pragma mark -
#pragma mark - 懒加载
- (SSRegisterNextView *)registerView{
    __weak typeof (self) weakSelf = self;
    if (!_registerView) {
        _registerView = [[SSRegisterNextView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        _registerView.phoneNumString = self.userInfo[@"userName"];
        _registerView.registerBlock = ^(NSString *code){
            [weakSelf registerMethod:code];
            
        };
    }
    return _registerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
